package leetcode.Y2024M5D27;

import org.junit.Test;

public class S92 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
       ListNode res=new ListNode(-1);
       res.next=head;
       ListNode pre=res;
       for (int i=0;i<left-1;i++){
           pre=pre.next;
       }

       ListNode cur=pre.next;
       ListNode next;
        for (int i = 0; i < right - left; i++) {
            next=cur.next;
            cur.next=next.next;
            next.next=pre.next;
            pre.next=next;
        }
        return res.next;

    }


    @Test
    public void test() {
        int[] head = {1, 2, 3, 4, 5};
        int left = 2;
        int right = 4;

        ListNode listNode = ListNode.createListNode(head);
        reverseBetween(listNode,left,right);
        System.out.println(listNode);
    }

    @Test
    public void test1() {
        int[] head = {3,5};
        int left = 1;
        int right = 1;

        ListNode listNode = ListNode.createListNode(head);
        reverseBetween(listNode,left,right);
        System.out.println(listNode);
    }
}
